package com.zxy.actspringboot.service;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : zhangxuyuan
 * @Project: activitiSpringbootTest
 * @Package com.zxy.actspringboot.service
 * @date Date : 2021年02月02日 10:27
 */
@Service
public class ActivitiWorkflowService {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;



    @PostConstruct
    public void deploy()
    {
//        Deployment evection = repositoryService.createDeployment().addClasspathResource("leaveSpringboot.bpmn")
//                .name("请假with监听").deploy();
//        System.out.println(evection.getId());
//        System.out.println(evection.getName());
    }

    public String startProcess(String key,String assign)
    {
        Map<String,Object> param=new HashMap<String, Object>();
//        param.put("user","jack");
//        param.put("user","xiaoma");
        //填入此流程相关用户，实际开发中一般要根据当前用户和流程查询出相关用户
        param.put("user",assign);
        param.put("leader","lee");
        param.put("manager","laoda");
        //业务id
        String businessKey="11111";
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey(key, businessKey, param);
        //完成第一个task
        Task task = taskService.createTaskQuery().taskAssignee(assign).
                processInstanceId(processInstance.getProcessInstanceId()).singleResult();
        taskService.complete(task.getId());
        return processInstance.getProcessInstanceId();
    }

    public String completeTask(String instanceId, String assign, Map<String,Object> params)
    {
        Task task = taskService.createTaskQuery().taskAssignee(assign)
                .processInstanceId(instanceId).singleResult();
        taskService.complete(task.getId(),params);
        return instanceId+" "+task.getId();
    }
}
